<?php
session_start();

	class block_twitter_write extends block_base
	{
		
		function init() {
        	// standard code to set the block title and version
        	$this->title = 'Post to Twitter';
        	$this->version = 2008120400;
	    }
		
	    function get_content()
	    {	    	
	    	if ($this->content !== NULL)
	    	{
	        	return $this->content;
	    	}
	
	    	$this->content = new stdClass;
	    	
	    	// To get wwwroot we must have access to the $CFG
			global $CFG;
			
			$thisroot = $CFG->wwwroot.$this->config->block_twitter_write_path;
						
			// write Javascript to handle the submission, and HTML to render the form
			// (the Javascript uses AJAX to send the data to lib.php, which actually posts the data to Twitter)
			$output = "<html>
				<body>
				<script type=\"text/javascript\">
				function updateFeed()
				{
					var xmlHttp;
					
					document.cookie = \"twitterWriteUser=\" + escape(document.updateform.twitterWriteUser.value) + \";path=/\";;
					document.cookie = \"twitterWritePass=\" + escape(document.updateform.twitterWritePass.value) + \";path=/\";;
					document.cookie = \"twitterWritePost=\" + escape(document.updateform.twitterWritePost.value) + \";path=/\";;
					
					try
					{
						xmlHttp = new XMLHttpRequest();
					}
					catch(e)
					{
						try
						{
							xmlHttp = new ActiveXObject(\"Msxml2.XMLHTTP\");
						}
						catch(e)
						{
							try
							{
								xmlHttp = new ActiveXObject(\"Microsoft.XMLHTTP\");
							}
							catch(e)
							{
								alert(\"Your browser does not support AJAX!\");
								return false;
							}
						}
					}
					
					xmlHttp.onreadystatechange=function()
					{
						if(xmlHttp.readyState==4)
						{
							document.getElementById(\"responsecontent\").innerHTML = xmlHttp.responseText;
						}
					}
					
					xmlHttp.open(\"GET\", \"". $thisroot ."lib.php\", true);
					xmlHttp.send(null);

				}				
								
				</script>
				
				<form name=\"updateform\">
   				Username: <input type=\"text\" name=\"twitterWriteUser\"/><br />
    			Password: <input type=\"password\" name=\"twitterWritePass\"/><p />
    			Your Message: <input type=\"text\" name=\"twitterWritePost\"/><p />
 				<input type=\"button\" value=\"Send Tweet\" onClick=\"updateFeed();\" />
    			</form>
    			
    			<hr />
				
	    		<span id=\"responsecontent\"></span>
				
				<script type=\"text/javascript\">
				
					document.updateform.twitterWriteUser.value = get_cookie(\"twitterWriteUser\");
					document.updateform.twitterWritePass.value = get_cookie(\"twitterWritePass\");
					
				</script>				
								
				</body>
				</html>";
				
			// set this Javascript/HTML content as the block's output, and display it
			$this->content->text = $output;
	    	return $this->content;
				
	    }

		function instance_allow_config()
		{
	    	// disallow instance-based block configuration
	    	return true;
		}
		
		function instance_allow_multiple()
		{
    		// disallow multiple instances of the block
    		return true;
		}
		
		function preferred_width() {
			// set the preferred value in pixels
			return 200;
	    }
	    
	}
	
?>